package com.itaq.cheetah.serviceA.utils;

import com.fasterxml.jackson.annotation.JsonInclude.Include;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.JavaType;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.PropertyNamingStrategy;
import com.itaq.cheetah.serviceA.config.factory.ObjectMapperFactory;
import lombok.experimental.UtilityClass;
import lombok.extern.slf4j.Slf4j;

import java.io.IOException;

/**
 * JSON的驼峰和下划线互转帮助类
 *
 * @author sch
 */
@Slf4j
@UtilityClass
public class JsonUtils {

    /**
     * 将对象的大写转换为下划线加小写，例如：userName-->user_name
     *
     * @param object
     * @return
     * @throws JsonProcessingException
     */
    public static String toUnderlineJSONString(Object object) throws JsonProcessingException {
        ObjectMapper mapper = new ObjectMapper();
        mapper.setPropertyNamingStrategy(PropertyNamingStrategy.SNAKE_CASE);
        mapper.setSerializationInclusion(Include.NON_NULL);
        String reqJson = mapper.writeValueAsString(object);
        return reqJson;
    }

    /**
     * 将下划线转换为驼峰的形式，例如：user_name-->userName
     *
     * @param json
     * @return
     * @throws IOException
     */
    public static <T> T toSnakeObject(String json, TypeReference<T> typeReference) {
        try {
            ObjectMapper mapper = new ObjectMapper();
            // mapper的configure方法可以设置多种配置（例如：多字段 少字段的处理）　　　　　　
            mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
            mapper.setPropertyNamingStrategy(PropertyNamingStrategy.SNAKE_CASE);
            return mapper.readValue(json, typeReference);
        } catch (JsonProcessingException e) {
            log.error("",e);
            return null;
        }
    }

    /**
     * 不抛出异常获取json传
     *
     * @param object
     * @return
     */
    public static String toString(Object object) {
        try {
            return ObjectMapperFactory.getObjectMapper().writeValueAsString(object);
        } catch (JsonProcessingException e) {
            log.error("", e);
            return "";
        }
    }

    public static <T> T read(String str, TypeReference<T> typeReference) {
        try {
            return ObjectMapperFactory.getObjectMapper().readValue(str, typeReference);
        } catch (JsonProcessingException e) {
            log.error("", e);
            return null;
        }
    }

    public static <T> T read(String str,Class<T> clazz){
        try {
            return ObjectMapperFactory.getObjectMapper().readValue(str, clazz);
        } catch (JsonProcessingException e) {
            log.error("", e);
            return null;
        }
    }

    public static String toPrettyString(Object object) {
        try {
            return ObjectMapperFactory.getObjectMapper().writerWithDefaultPrettyPrinter().writeValueAsString(object);
        } catch (JsonProcessingException e) {
            log.error("", e);
            return "";
        }
    }

    public static Object readValue(String s, JavaType contextualType) {
        try {
            return ObjectMapperFactory.getObjectMapper().readValue(s, contextualType);
        } catch (JsonProcessingException e) {
            log.error("", e);
            return null;
        }
    }
}